global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A6_baseline_inventor_vs_hqctry_combined.log, replace name(tabl)

capture noi {
    
    * Table_baseline with inventorcountry(pre sample) and HQ country fixed effects instead of the ususal home country fixed effects 

    qui do ${code_dir}/config/tabletools.do

    use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear

    *  necessary as the countrylists are not identical between homecountry and inventorhomecountry
    mmerge BvDIDnumber year using ${final_dir}/supplement_dataset_from1970_tfacit1_hinvt_ctry_restricted.dta, unmatched(master) 
    mmerge BvDIDnumber using ${dataset_dir}/weights/bvdid_main_inventor_all_from1970_1995_orbis2017_restricted.dta, unmatched(master)

    drop _merge

    tab year, gen(YY_)

    estimates clear
    foreach yy in 1995 {	
        clonevar LSW = lswMPm_`yy'_a
        clonevar HSW = hswMPm_`yy'_a
        clonevar VAEMP = vaempMPm_`yy'_a
        clonevar GDPGAP = lngdpgap_`yy'_a
        clonevar GDPPC = gdppcMPm_`yy'_a
        clonevar stockown = k${depvar}_${ttt} 
        clonevar stockownzero = k${depvar}_${ttt}0  
        clonevar stockother = kNOT_${depvar}_${ttt} 
        clonevar stockotherzero = kNOT_${depvar}_${ttt}0
        clonevar spilloversown = spill${depvar}${ttt}_`yy'_a
        clonevar spilloversother = spillN${depvar}${ttt}_`yy'_a
        clonevar spilloversownzero = spill${depvar}${ttt}_`yy'_a0
        clonevar spilloversotherzero = spillN${depvar}${ttt}_`yy'_a0
        bys lse_id : egen _total_${depvar}_${ttt}_`yy' = sum(${depvar}_${ttt}) if year>=`yy'+2 & year <= 2009+2
        bys lse_id : egen total_${depvar}_${ttt}_`yy' = max(_total_${depvar}_${ttt}_`yy')
        drop _total_${depvar}_${ttt}_`yy'
        egen yearctry = group(year main_inventor_country) if year <= 2009
        egen yearctryindustry = group(year industry main_inventor_country) if year <= 2009

    
        egen yearindustry = group(year industry) if year <= 2009
        sort lse_id year

        *the inventorcountry FE columns
        * 1) GDPGAP | F+IY+CY
        ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_1
        * 2) GDPGAP + VAEMP | F+IY+CY
        ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_2
        * 3) GDPGAP + GDPPC | F+IY+CY
        ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_3
    

        clonevar GDPPC_foreign = gdppcMPm_shr4_fginvt_`yy'_a
        clonevar VAEMP_foreign = vaempMPm_shr4_fginvt_`yy'_a 
        clonevar GDPgap_foreign = lngdpgap_shr_fginvt_`yy'_a
        clonevar LSW_foreign = lswMPm_shr4_fginvt_`yy'_a
        clonevar HSW_foreign = hswMPm_shr4_fginvt_`yy'_a

        * 4) GDPGAP | F+IY+CY | foreign
        ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_4
        * 5) GDPGAP + VAEMP | F+IY+CY | foreign
        ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_5
        * 6) GDPGAP + GDPPC | F+IY+CY | foreign
        ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign GDPPC_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_6
    }



    
    drop _all

    use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
    mmerge BvDIDnumber year using ${final_dir}/supplement_dataset_from1970_tfacit1_hqctry_restricted.dta, unmatched(master)
    drop _merge
    gen hq_ctry = substr(BvDIDnumber,1,2)

    tab year, gen(YY_)


    foreach yy in 1995 {	
        clonevar LSW = lswMPm_`yy'_a
        clonevar HSW = hswMPm_`yy'_a
        clonevar VAEMP = vaempMPm_`yy'_a
        clonevar GDPGAP = lngdpgap_`yy'_a
        clonevar GDPPC = gdppcMPm_`yy'_a
        clonevar stockown = k${depvar}_${ttt} 
        clonevar stockownzero = k${depvar}_${ttt}0  
        clonevar stockother = kNOT_${depvar}_${ttt} 
        clonevar stockotherzero = kNOT_${depvar}_${ttt}0
        clonevar spilloversown = spill${depvar}${ttt}_`yy'_a
        clonevar spilloversother = spillN${depvar}${ttt}_`yy'_a
        clonevar spilloversownzero = spill${depvar}${ttt}_`yy'_a0
        clonevar spilloversotherzero = spillN${depvar}${ttt}_`yy'_a0
        bys lse_id : egen _total_${depvar}_${ttt}_`yy' = sum(${depvar}_${ttt}) if year>=`yy'+2 & year <= 2009+2
        bys lse_id : egen total_${depvar}_${ttt}_`yy' = max(_total_${depvar}_${ttt}_`yy')
        drop _total_${depvar}_${ttt}_`yy'
        egen yearctry_hq= group(year hq_ctry) if year <= 2009
        egen yearctryindustry_hq= group(year industry hq_ctry) if year <= 2009
        egen yearindustry = group(year industry) if year <= 2009
        sort lse_id year

        *the HQ FE country columns

        * 7) GDPGAP | F+IY+CY
        ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry_hq) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_7
        * 8) GDPGAP + VAEMP | F+IY+CY
        ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry_hq) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_8
        * 9) GDPGAP + GDPPC | F+IY+CY
        ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry_hq) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_9

        clonevar GDPPC_foreign = gdppcMPm_shr4_nonhq_`yy'_a	
        clonevar VAEMP_foreign = vaempMPm_shr4_nonhq_`yy'_a
        clonevar GDPgap_foreign = lngdpgap_shr_nonhq_`yy'_a
        clonevar LSW_foreign = lswMPm_shr4_nonhq_`yy'_a
        clonevar HSW_foreign = hswMPm_shr4_nonhq_`yy'_a

        * 10) GDPGAP | F+IY+CY | foreign
        ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry_hq) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_10
        * 11) GDPGAP + VAEMP | F+IY+CY | foreign
        ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry_hq) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_11
        * 12) GDPGAP + GDPPC | F+IY+CY | foreign
        ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign GDPPC_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`yy' & missing_weights_`yy'==0 & missing_spill_weights_`yy' == 0 & maxweight_`yy' < 1 & total_${depvar}_${ttt}_`yy'>0,absorb(lse_id yearindustry yearctry_hq) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local stock_spill "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_12
    }

    setlabels
    qui do ${code_dir}/config/tabletools.do

    dhoztab * using ${tab_dir}/appendix/Table_A6_baseline_inventor_vs_hqctry_combined.tex, b(%10.2f) se(%10.2f) nogaps nomtitles numbers nolines nonotes replace  showdepvarinfo sfmt(a1) scalars("stock_spill \noalign{\medskip} Stocks and spillovers" "f Firm f.e." "iy Industry \stimes year f.e." "cy Country \stimes year f.e." "obs \noalign{\medskip} Observations"  "firms Number of firms") label mgroups("Dom. and Fgn." "Fgn." "Dom. and Fgn." "Fgn.", pattern(1 0 0 1 0 0 1 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(*zero _cons stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) depvar("Auto95") order(LSW HSW GDPGAP VAEMP GDPPC) rawaddheader(&\multicolumn{6}{c}{home = largest inventor weight}&\multicolumn{6}{c}{home = hq country} \\ \cmidrule(lr){2-7}\cmidrule(lr){8-13}) notes("This table varies the definition of the home country. In Columns 1-6, the home country is defined as the country with the largest inventor weight. In Columns 7-12, the home country is the one where headquarters are located. All columns include controls for stocks and spillovers, firm, industry-year and country-year fixed effects.  In Columns 4-6 and 9-12, the macroeconomic variables are the normalized foreign variables as defined in the text. Standard errors are clustered at the firm-level and reported in parentheses.")
    *again, brackets do work, just confuses the texteditor
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}